import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { LayerService } from './layer.service';
import { StyleService } from './style.service';
import { MapService } from './map.service';
import { LayerQueryService } from './layer.query.service';

@Injectable({
  providedIn: 'root'
})
export class CommonService {

  public fhHost = "http://test.cityfun.com.cn/api/test/pipeline";
  public problemSubmitURL = this.fhHost + "/RemarkDataPost";
  public problemSubmitPicURL = this.fhHost + "/PicDataPost";
  // public problemListURL = this.fhHost + "/RemarkDataListQuery";
  public problemListURL = 'mock-data/remark.list.json';
  public queryIQuerylist = 'mock-data/layer.list.json';
  public queryDetailListURL = 'mock-data/detail.list.json';


  public problemdetailURL = this.fhHost + "/RemarkDataDetailQuery";
  public problemDeleteURL = this.fhHost + "/RemarkDataDel";
  public problemDeletePicURL = this.fhHost + "/PicDataDelete";

  public basicDataQueryURL = this.fhHost + "/BasicDataQuery";

  constructor(
    private http: HttpClient,
    private layerOperaService: LayerService,
    private mapService: MapService,
    private layerQueryService: LayerQueryService,
    private styleService: StyleService
  ) { }
  // 问题属性上报
  public submitQuestion(body) {
    const httpOption = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };
    return this.http.post<any>(this.problemSubmitURL, body, httpOption)
    // return this.dataRxInquireService.post('common', 'problem.submit', body, null, httpOption);
  }
  // 问题图片提交
  public submitQuestionImg(body) {
    const httpOption = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };
    return this.http.post<any>(this.problemSubmitPicURL, body, httpOption)
    // return this.dataRxInquireService.post('common', 'problem.submit.img', body, null, httpOption);
  }
  // i查询详情
  public queryDetail(body) {
    const httpOption = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };
    return this.http.post<any>(this.queryDetailListURL, body, httpOption)
  }
  public getListData(params) {
    return this.http.get<any>(this.problemListURL, { params })
    // return this.dataRxInquireService.get('common', 'problem.list', null, params);
  }
  // 获取问题geojson数据绘制图层
  public getQuestionGeojsonData() {
    return this.http.get<any>(this.problemListURL)
    // return this.dataRxInquireService.get('common', 'problem.geojson.list', null, null);
  }
  // 查看问题详情
  public getQuestionDetail(params) {
    return this.http.get<any>(this.problemdetailURL, { params })
  }
  // 删除问题标注
  public delQuestionMark(params) {
    return this.http.delete<any>(this.problemDeleteURL, { params })
  }
  // 删除问题标注图片
  public delQuestionPic(params) {
    return this.http.delete<any>(this.problemDeletePicURL, { params })
  }
  // i查询图层列表
  public getIQuerylist() {
    return this.http.get<any>(this.queryIQuerylist)
  }

  // i查询图层列表
  public basicDataQuery(params) {
    return this.http.get<any>(this.basicDataQueryURL, { params })
    // return this.dataRxInquireService.get('common', 'sourceLayer.list', null, null);
  }




  // 提交图片返回处理结果
  backImgResult(e, collback) {
    const params = {
      fileName: e.imgData.name,
      data: e.imgData.url,
      relateId: e.remoarkid
    };
    this.submitQuestionImg(params).subscribe(res => {
      if (res.code === 1) {
        // this.imginfo = res.data;
        collback(res.data);
      } else {
        // this.imginfo = null;
        collback(null);
      }
      // 绘制circle
    });
  }
  // 获取问题列表接口数据绘制问题图层
  getQuestionGeojson() {
    this.getQuestionGeojsonData().subscribe(res => {
      if (res.code === 1) {
        const data = res.data.features;
        // TODO: 为每一个元素proprietary属性添加一个序号（id）属性:目的，地图上的问题标记点上面显示序号
        data.forEach((element, i) => {
          element.properties.id = '';
        });
        // 绘制
        this.layerOperaService.drawQuestionGeo(data);
      } else {
        const removeLayerID = this.styleService.getQuestionLayer();
        this.mapService.removeLayerByIds(removeLayerID);
      }
      // 绘制circle
    });
  }
  getSourceLayerID() {
    this.getIQuerylist().subscribe(res => {
      if (res.code === 1) {
        // 获取的是所有可以执行i查询的图层列表
        this.layerOperaService.getSourceLayerList(res.data);
      } else {
        // 否则清空这个i查询的图层列表
        this.layerQueryService.clearLayer();
      }
    });
  }
  getPropertyListx(fs, e, map, callback) {
    if (fs && fs[0]) {
      const disctoryArr = [];
      const item = { title: fs[0].title, property: fs[0].properties };
      const itemFeature = { geo: fs[0].geometry, itemData: item };
      disctoryArr.push(itemFeature);
      callback(disctoryArr);
      // 绘制circle
      this.layerOperaService.drawCircle(fs);
      return
    }

    callback(null);
    // 清空上次绘制的circle（也就是那个高亮的图层）
    const layers = ['highlightPointID', 'highlightLineID', 'lightLinetyleID'];
    this.mapService.removeLayerByIds(layers);
    return
  }
  getPropertyList(layeInfo, e, map, callback) {

    this.queryDetail(this.layerOperaService.getLayerIdS(layeInfo)).subscribe(res => {
      if (res.code === 1 && res.data) {
        this.layerOperaService.constroctroertiesList(map, res.data.features, e, (d) => {
          callback(d);
          //  this.disctoryArr = d;
        });
        // 绘制circle
        this.layerOperaService.drawCircle(res.data.features);
      } else {
        callback(null);
        // this.disctoryArr = null;
      }
      // 相当于没取到数据
      if (layeInfo.length === 0) {
        // 清空上次绘制的circle（也就是那个高亮的图层）
        const layers = ['highlightPointID', 'highlightLineID', 'lightLinetyleID'];
        this.mapService.removeLayerByIds(layers);
      }
      // 绘制circle
    });
  }
  isOpenLayer(map, id) {
    if (map && map.getLayer(id)) {
      return true;
    } else {
      return false;
    }
  }
}
